package com.sjqi.demo.executor;

import com.sjqi.demo.config.Configuration;
import com.sjqi.demo.sqlsource.SqlSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qishaojun
 * SimpleExecutor:普通JDBC
 * BatchExecutor:批处理
 * ReuseExecutor:statement可重用
 * 都会去走该类的一级缓存的流程
 */
public abstract class BaseExecutor implements Executor {
    private Map<String, List<Object>> oneLevelCache = new HashMap<>();

    @Override
    public <T> List<T> query(Configuration configuration, MappedStatement mapStatement,Object param) {
        SqlSource sqlSource=mapStatement.getSalSource();
        BoundSql boundSql=sqlSource.getBoundSql(param);

        //获取缓存的key
        String cacheKey = getCacheKey(boundSql);
        List<Object> list = oneLevelCache.get(cacheKey);
        if (null != list) {
            return (List<T>) list;
        }
        list=queryFromDataBase(Configuration configuration, MappedStatement mapStatement);
        return null;
    }

    protected String getCacheKey(BoundSql boundSql) {
     return boundSql.getSql();
    }
    abstract List<Object>  queryFromDataBase(Configuration configuration, MappedStatement mapStatement);
}
